"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const defines_1 = require("../lib/defines");
function leaveClassroom(app) {
    const { socket, rooms } = app.locals;
    const { userId } = socket.handshake.query;
    socket.on(defines_1.RequestMethod.leave, function leave({ room, member }, cb) {
        const __room = rooms[room.classId];
        if (__room) {
            const { creator } = __room;
            room = Object.assign(room, __room.json());
            socket.broadcast
                .timeout(500)
                .to(room.classId)
                .emit('left', { room, member }, function (error) {
                if (member.userId === creator.userId && member.userId === userId) {
                    __room.members[userId].stop();
                    __room.getMembers().forEach(member => {
                        member.stop(userId);
                    });
                }
                else {
                    __room.members[member.userId].stop(userId);
                }
                if (cb) {
                    cb({ status: 'ok', resolve: 'dissolved' });
                }
            });
        }
        else {
            console.error('room is not find!');
        }
    });
}
exports.default = leaveClassroom;
